(function () {
    var $tbody = $("#list-body"),
    	$searchForm = $("#search-form"),
    	$addForm = $("#add-form"),
        trHtml = undefined,
        stations = [],
        domain = "http://localhost:8080/api/v2/boss/",
        curPage = 1;
    var _this = {
    	// 表格初始化 
        list : function(params){
        	params = params || {};
			params.page = curPage;
    		App.ajax({
                data: params,
                resUrl: domain + "commentTag/page",
                callback: function (rb) {
                    if(rb.code != 1000) return;
                    
                    $tbody.empty();
                    $.each(rb.data.list||[], function(i, d){
                    	var $tr = $(trHtml);
                    	
                    	$tr.setHtml(d).data(d);
						$tr.attr({id:"tr_commentTag_"+d.id});
						
						$tr.find("[html-tagType]").html(d.tagType == 1? "正面": "负面" );
						$tr.find("[html-enabled]").html(d.enabled == 2? "否": "是");
						$tr.find("[html-updateTime]").html(App.formatDate(d.updateTime));
						$tr.find('[btn-type="enabled"]').addClass(d.enabled == 1? "am-text-danger": "am-text-secondary");
						$tr.find('[btn-type="enabled"]').html(d.enabled == 1? '<span class="am-icon-toggle-off"></span> 禁用': '<span class="am-icon-toggle-off"></span> 启用');
						
                    	$tbody.append($tr);
                    });

					require(['laypage'], function (laypage) {
						var data = rb.data || {};
						laypage({
							cont: 'page-body', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
							pages:data.totalPage, //通过后台拿到的总页数
							curr: data.pageNumber || 1, //当前页
							jump: function (obj, first) { //触发分页后的回调
								curPage = obj.curr;
								if (!first) {
									_this.list();
								}
							}
						});
					});
                }
    		}); 
        },
        submitForm: function(opt){
			var $form = opt.selector;
			var data = opt.data || {};
			var type = opt.type || "add";
			var cb = opt.cb || function(){};

			var postData = $form.serializeObject();
			postData = $.extend({}, data, postData);

			if(!postData.tagType){
				layer.msg('请选择标签类型', {icon: 2});
				return false;
			}
			if(!postData.tagName){
				layer.msg('请输入标签名称', {icon: 2});
				return false;
			}
			if(!postData.enabled){
				layer.msg('请选择是否启用', {icon: 2});
				return false;
			}
			if(!postData.sequence){
				layer.msg('请数序标签顺序', {icon: 2});
				return false;
			}

			App.ajax({
				data: postData,
				resUrl: domain+"commentTag/add",
				callback: function (rb) {
					if(rb.code != 1000) {
						layer.msg(rb.msg||'操作不成功', {icon: 5});
						return;
					}
					layer.msg('成功', {icon: 1});
					_this.list();
					!!cb && cb();
				}
			});
		},
        // 事件初始化
        initEvent : function(){
        	// 查询初始化
            $searchForm.submit(function(){
            	var params = $searchForm.serializeObject();
				curPage = 1;
				_this.list(params);
				return false;
			});
            // 新增初始化
            $addForm.submit(function(){
				var $form = $addForm;
				_this.submitForm({
					selector: $form,
					type: "add",
					cb: function(){
						$('#add-div').slideToggle();
					}
				});
				return false;
			});
			// 禁用启用点击
          	App.delegate({
			    btntype: "[btn-type=enabled]",
			    callback: function (data, $tr) {
			    	var id = $(this).parents('tr').data('id');
			    	if( $tr.find('[btn-type=enabled]').hasClass('am-text-danger') ){
			    		var msg = '你确定禁用此评论标签吗？';
			    		var action = "disable";
			    		var enabled = 2;
			    	}else{
			    		var msg = '你确定启用此评论标签吗？';
			    		var action = "enable";
			    		var enabled = 1;
			    	};
	            	var btnok = '确认！'
	            	var _index = layer.confirm('<div style="margin: auto;width: 222px;">'+msg+'</div>', {
						type:1,
						title:"二次确认",
						area: ['300px', 'auto'], //宽高
						btn: [btnok, '再考虑下']
					}, function(){
						layer.close(_index);
	
						var postData = { id: data.id };
						App.ajax({
							data: postData,
							resUrl: domain+"commentTag/"+action,
							callback: function (rb) {
								if(rb.code != 1000) {
									layer.msg(rb.msg||'操作不成功', { icon: 5 });
									return;
								}
								layer.msg('成功', { icon: 1 });
								data.enabled = enabled;
								$tr.data(data);
								$tr.find("[html-enabled]").html(data.enabled == 2? "否": "是");
								$tr.find('[btn-type="enabled"]').removeClass("am-text-danger");
								$tr.find('[btn-type="enabled"]').removeClass("am-text-secondary");
								$tr.find('[btn-type="enabled"]').addClass(data.enabled == 1? "am-text-danger": "am-text-secondary");
								$tr.find('[btn-type="enabled"]').html(data.enabled == 1? '<span class="am-icon-toggle-off"></span> 禁用': '<span class="am-icon-toggle-off"></span> 启用');
							}
						});
					}, function(){
						// 取消
						layer.close(_index);
					});
				}
			});
			// 删除
            $("#list-body").delegate("[btn-type=delete]",'click',function(){
            	var id = $(this).parents('tr').data('id');
            	var msg = '是否确认删除标签？';
            	var btnok = '确认！'
            	var _index = layer.confirm('<div style="margin: auto;width: 222px;">'+msg+'</div>', {
					type:1,
					title:"二次确认",
					area: ['300px', 'auto'], //宽高
					btn: [btnok, '再考虑下']
				}, function(){
                        // 确定后的删除操作，取到id，调用删除接口，成功后刷新表格
                        App.ajax({
                        	data:{id: id},
                        	resUrl: domain+'commentTag/delete',
                        	callback: function(rb){
                        		if(rb.code != 1000) {
                                    layer.msg(rb.msg, { icon: 5 });
                                    return;
                                }else{
                                	layer.msg(rb.msg, { icon: 5 });
                                	_this.list();
			                        layer.close(_index);
                                }
                        	}
                        });
				}, function(){
					// 取消
					layer.close(_index);
				});
          	});
        },
        init : function () {
            trHtml = $tbody.html();
            $tbody.empty().show();
            _this.initEvent();
            _this.list();
        }
    }
    _this.init(); //页面初始化入口设置
})();
